package cn.mifengkong.appcommon.webview.jshandler;

import android.support.annotation.NonNull;

import com.mifengkong.frtools.jsbridge.CallBackFunction;
import com.mifengkong.frtools.jsbridge.JsHandler;

import org.json.JSONException;
import org.json.JSONObject;

import cn.mifengkong.appcommon.webview.InfoToJsonUtil;
import cn.mifengkong.appcommon.webview.uicomponent.WebviewDataConfig;


/**
 * Created by jiangyongxing on 2017/10/9.
 * 描述：
 */

public class GetDataJSHandler implements JsHandler {

    private static final String USERINFO = "userInfo";//获取速贷的用户数据
    private static final String JJUSERINFO = "jjuserinfo";//获取极简信用卡的用户数据
    private static final String VERSION = "version";
    private static final String DEVICE = "device";
    private String mType;

    private WebviewDataConfig mWebviewDataConfig;

    public GetDataJSHandler(WebviewDataConfig webviewDataConfig) {
        mWebviewDataConfig = webviewDataConfig;
    }


    @Override
    public void OnHandler(String handlerName, String responseData, CallBackFunction function) {
        parseResponseData(responseData);
        InfoToJsonUtil.assemblyIntoJson(function, setData(mType));

    }

    private void parseResponseData(String responseData) {
        try {
            JSONObject responseDataJO = new JSONObject(responseData);
            mType = responseDataJO.getString("type");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @NonNull
    private JSONObject setData(String responseData) {
        if (null != mWebviewDataConfig) {
            JSONObject date = new JSONObject();
            try {
                switch (responseData) {
                    case USERINFO:
                        date.put("user_id", mWebviewDataConfig.getUserId());
                        date.put("phone", mWebviewDataConfig.getPhone());
                        date.put("token", mWebviewDataConfig.getToken());
                        break;
                    case DEVICE:
                        date.put("app_union_name", mWebviewDataConfig.getAppUnionName());
                        date.put("app_name", mWebviewDataConfig.getAppName());
                        date.put("net_info", mWebviewDataConfig.getNetInfo());
                        date.put("dev_sys", mWebviewDataConfig.getAndroidVersion());
                        date.put("screenSize", mWebviewDataConfig.getScreentSzie());
                        date.put("mobileOperators", mWebviewDataConfig.getMobleOperators());
                        date.put("countryCode", mWebviewDataConfig.getCountryCode());
                        date.put("languageCode", mWebviewDataConfig.getLanguageCode());
                        date.put("capcitySize", mWebviewDataConfig.getEquipmentCapacity());
                        date.put("dev_no", mWebviewDataConfig.getModel());
                        date.put("dev_brand", mWebviewDataConfig.getDevBrand());
                        date.put("lat", mWebviewDataConfig.getLatitude());
                        date.put("lng", mWebviewDataConfig.getLongitude());
                        //不需要break，后面的数据 version中的数据  device也会用到
                    case VERSION:
                        date.put("plat_type", mWebviewDataConfig.getPlatType());
                        date.put("plat_version", mWebviewDataConfig.getPlatVersion());
                        date.put("package_name", mWebviewDataConfig.getPackageName());
                        date.put("source_id", mWebviewDataConfig.getSourceId());
                        date.put("channel_id", mWebviewDataConfig.getSourceId());
                        date.put("uuid", mWebviewDataConfig.getUUID());
                        break;
                    case JJUSERINFO:
                        date.put("user_id", mWebviewDataConfig.getUserIdForJJ());
                        date.put("phone", mWebviewDataConfig.getPhoneForJJ());
                        date.put("token", mWebviewDataConfig.getTokenForJJ());
                        break;
                    default:
                        break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return date;
        }
        return null;
    }
}
